<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="请求主机" prop="requestHost">
        <el-input
          v-model="queryParams.requestHost"
          placeholder="请输入请求主机"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请求类型" prop="requestMethod">
        <el-input
          v-model="queryParams.requestMethod"
          placeholder="请输入请求类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="返回状态" prop="responseStatus">
        <el-select v-model="queryParams.responseStatus" placeholder="请选择返回状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="服务名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入网关服务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网关服务端口号" prop="servicePort">
        <el-input
          v-model="queryParams.servicePort"
          placeholder="请输入网关服务端口号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路由id" prop="routeId">
        <el-input
          v-model="queryParams.routeId"
          placeholder="请输入路由id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路由名称" prop="routeName">
        <el-input
          v-model="queryParams.routeName"
          placeholder="请输入路由名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路由主机名" prop="routeHost">
        <el-input
          v-model="queryParams.routeHost"
          placeholder="请输入路由主机名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['log:log:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="创建时间" align="center" width="180" prop="createTime" />
      <el-table-column label="客户端ip" align="center" width="200" prop="clientIp" />
      <el-table-column label="请求路径" align="center" width="200" prop="requestUri" />
      <el-table-column label="请求类型" align="center" prop="requestMethod" />
      <el-table-column label="请求主机" align="center" prop="requestHost" />
      <el-table-column label="返回状态" align="center" prop="responseStatus" />
      <el-table-column label="请求参数" align="center" prop="requestQuerystring" />
      <el-table-column label="请求路径" align="center"  width="200"   prop="requestUrl"  />
      <el-table-column label="请求体" align="center" prop="requestBody"  width="150">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top" width="600">
            <p>{{scope.row.requestBody}}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{scope.row.requestBody}}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="响应体" align="center" prop="responseBody" width="150">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top" width="600">
            <p>{{scope.row.responseBody}}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{scope.row.responseBody}}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="请求大小" align="center" prop="requestSize" />
      <el-table-column label="响应大小" align="center" prop="responseSize" />
      <el-table-column label="服务id" align="center" prop="serviceId" />
      <el-table-column label="路由id" align="center" prop="routeId" />
      <el-table-column label="客户id" align="center" prop="consumerId" />
      <el-table-column label="服务端口号" align="center" prop="servicePort" />
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column label="路由名称" align="center" prop="routeName" />
      <el-table-column label="kong网关主机名" align="center" prop="serviceHost" />
      <el-table-column label="网关服务路径" align="center" prop="servicePath" />
      <el-table-column label="服务tag" align="center" prop="serviceTags" />
      <el-table-column label="路由主机名" align="center" prop="routeHost" />
      <el-table-column label="客户名称" align="center" prop="consumerName" />
      <el-table-column label="负载信息列表" align="center" prop="triesIp" />
      <el-table-column label="上游uri" align="center" prop="upstreamUri" />
      <!-- <el-table-column label="路由对应服务的id" align="center" prop="routeServiceId" /> -->
      <!-- <el-table-column label="网关空间id" align="center" prop="serviceWsId" /> -->
      <!-- <el-table-column label="是运行所有插件所需的内部 Kong 延迟" align="center" prop="latenciesKong" /> -->
      <!-- <el-table-column label="是从客户端读取第一个字节和最后一个字节发送到客户端之间经过的时间" align="center" prop="latenciesRequest" />
      <el-table-column label="是最终服务处理请求所用的时间" align="center" prop="latenciesLatencies" /> -->
      <!-- <el-table-column label="开始处理请求时的 UTC 时间" align="center" prop="startedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份验证的凭据信息" align="center" prop="authenticatedEntity" />
      <el-table-column label="身份验证的凭据信息" align="center" prop="remark" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['log:log:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['log:log:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改kong网关转发日志记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="请求主机" prop="requestHost">
          <el-input v-model="form.requestHost" placeholder="请输入请求主机" />
        </el-form-item>
        <el-form-item label="请求路径" prop="requestUrl">
          <el-input v-model="form.requestUrl" placeholder="请输入请求路径" />
        </el-form-item>
        <el-form-item label="请求类型" prop="requestMethod">
          <el-input v-model="form.requestMethod" placeholder="请输入请求类型" />
        </el-form-item>
        <el-form-item label="请求大小" prop="requestSize">
          <el-input v-model="form.requestSize" placeholder="请输入请求大小" />
        </el-form-item>
        <el-form-item label="请求路径" prop="requestUri">
          <el-input v-model="form.requestUri" placeholder="请输入请求路径" />
        </el-form-item>
        <el-form-item label="是运行所有插件所需的内部 Kong 延迟" prop="latenciesKong">
          <el-input v-model="form.latenciesKong" placeholder="请输入是运行所有插件所需的内部 Kong 延迟" />
        </el-form-item>
        <el-form-item label="是从客户端读取第一个字节和最后一个字节发送到客户端之间经过的时间" prop="latenciesRequest">
          <el-input v-model="form.latenciesRequest" placeholder="请输入是从客户端读取第一个字节和最后一个字节发送到客户端之间经过的时间" />
        </el-form-item>
        <el-form-item label="是最终服务处理请求所用的时间" prop="latenciesLatencies">
          <el-input v-model="form.latenciesLatencies" placeholder="请输入是最终服务处理请求所用的时间" />
        </el-form-item>
        <el-form-item label="返回状态">
          <el-radio-group v-model="form.responseStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="响应内容大小" prop="responseSize">
          <el-input v-model="form.responseSize" placeholder="请输入响应内容大小" />
        </el-form-item>
        <el-form-item label="网关服务id" prop="serviceId">
          <el-input v-model="form.serviceId" placeholder="请输入网关服务id" />
        </el-form-item>
        <el-form-item label="网关服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入网关服务名称" />
        </el-form-item>
        <el-form-item label="kong网关主机名" prop="serviceHost">
          <el-input v-model="form.serviceHost" placeholder="请输入kong网关主机名" />
        </el-form-item>
        <el-form-item label="网关服务路径" prop="servicePath">
          <el-input v-model="form.servicePath" placeholder="请输入网关服务路径" />
        </el-form-item>
        <el-form-item label="网关服务端口号" prop="servicePort">
          <el-input v-model="form.servicePort" placeholder="请输入网关服务端口号" />
        </el-form-item>
        <el-form-item label="网关空间id" prop="serviceWsId">
          <el-input v-model="form.serviceWsId" placeholder="请输入网关空间id" />
        </el-form-item>
        <el-form-item label="路由id" prop="routeId">
          <el-input v-model="form.routeId" placeholder="请输入路由id" />
        </el-form-item>
        <el-form-item label="路由名称" prop="routeName">
          <el-input v-model="form.routeName" placeholder="请输入路由名称" />
        </el-form-item>
        <el-form-item label="路由主机名" prop="routeHost">
          <el-input v-model="form.routeHost" placeholder="请输入路由主机名" />
        </el-form-item>
        <el-form-item label="路由对应服务的id" prop="routeServiceId">
          <el-input v-model="form.routeServiceId" placeholder="请输入路由对应服务的id" />
        </el-form-item>
        <el-form-item label="客户id" prop="consumerId">
          <el-input v-model="form.consumerId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="客户名称" prop="consumerName">
          <el-input v-model="form.consumerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="上游uri" prop="upstreamUri">
          <el-input v-model="form.upstreamUri" placeholder="请输入上游uri" />
        </el-form-item>
        <el-form-item label="客户端ip" prop="clientIp">
          <el-input v-model="form.clientIp" placeholder="请输入客户端ip" />
        </el-form-item>
        <el-form-item label="开始处理请求时的 UTC 时间" prop="startedAt">
          <el-date-picker clearable size="small"
            v-model="form.startedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始处理请求时的 UTC 时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份验证的凭据信息" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入身份验证的凭据信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLog, getLog, delLog, addLog, updateLog, exportLog } from "@/api/kong/log.js";
import moment from 'moment'

export default {
  name: "Log",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // kong网关转发日志记录表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requestHost: null,
        requestMethod: null,
        responseStatus: null,
        serviceName: null,
        serviceHost: null,
        servicePath: null,
        servicePort: null,
        routeId: null,
        routeName: null,
        routeHost: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询kong网关转发日志记录列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        requestQuerystring: null,
        requestHost: null,
        requestUrl: null,
        requestMethod: null,
        requestSize: null,
        requestBody: null,
        requestUri: null,
        latenciesKong: null,
        latenciesRequest: null,
        latenciesLatencies: null,
        responseStatus: 0,
        responseSize: null,
        responseBody: null,
        serviceId: null,
        serviceName: null,
        serviceHost: null,
        servicePath: null,
        servicePort: null,
        serviceWsId: null,
        serviceTags: null,
        routeId: null,
        routeName: null,
        routeHost: null,
        routeServiceId: null,
        consumerId: null,
        consumerName: null,
        triesIp: null,
        upstreamUri: null,
        clientIp: null,
        startedAt: null,
        authenticatedEntity: null,
        createTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加kong网关转发日志记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改kong网关转发日志记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLog(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLog(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除kong网关转发日志记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有kong网关转发日志记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportLog(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },    
    //时间处理函数
    dateFormat(row, column){
        const _date = row[column.property]*1000; 
        if(_date ==undefined){return ''};
        return moment(_date).format("YYYY-MM-DD HH:mm:ss");
    },
    dateFormatLong(_date){
        if(_date ==undefined){return ''};
        return moment(_date*1000).format("YYYY-MM-DD HH:mm:ss");
    },
  }
};
</script>
